<template>
    <div>
        <van-form @submit="onSubmit">
            <van-cell-group inset class="cell">
                <van-field v-model="loginname" name="用户名" label="用户名" placeholder="用户名"
                    :rules="[{ required: true, message: '请填写用户名' }]" />
                <van-field v-model="password" type="password" name="密码" label="密码" placeholder="密码"
                    :rules="[{ required: true, message: '请填写密码' }]" />
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    提交
                </van-button>
            </div>
        </van-form>
    </div>
</template>
<script lang='ts' setup>
import { ref } from 'vue';
import { login } from '@/service/api';
import { showNotify } from 'vant';
import 'vant/es/notify/style';
import router from '@/router';

const loginname = ref('');
const password = ref('');
async function onSubmit() {
    const result = await login(loginname.value, password.value)
    if (result.data.code !== '200') {
        showNotify({ type: 'danger', message: `用户名或密码错误`, duration: 1500 });
    } else {
        showNotify({ type: 'success', message: `${result.data.message}`, duration: 1500 });
        localStorage.setItem('token', result.data.data.token);
        localStorage.setItem('userid', result.data.data.userid);
        router.push('/self')
    }

}
</script>
<style scoped>
.cell {
    margin: 0;
    margin-top: 15px;
    border-radius: 0;
}
</style>